package cn.edu.nuc.onlinestore.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import UtilSever.Util;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.network.TCPClient;
import cn.edu.nuc.onlinestore.vo.Result;
import demo.factor.Ifactor.IFactory;

public class AdminAdd extends JFrame {

	private JPanel contentPane;
	private JTextField goodname;
	private JTextField price;
	private JTextField number;
	private JTextArea message;
	private JFrame frame = null;
	private JFrame store;
	private JFrame adminlogin;
	
	
	public JFrame getAdminlogin() {
		return adminlogin;
	}


	public void setAdminlogin(JFrame adminlogin) {
		this.adminlogin = adminlogin;
	}


	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminAdd frame = new AdminAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/


	public JFrame getStore() {
		return store;
	}


	public void setStore(JFrame store) {
		this.store = store;
	}


	/**
	 * Create the frame.
	 */
	public AdminAdd(/*JFrame store*/) {
		this.frame=this;
		setTitle("添加商品");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 405);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("商品名称:");
		label.setBounds(35, 57, 67, 15);
		contentPane.add(label);
		
		goodname = new JTextField();
		goodname.setBounds(99, 54, 269, 21);
		contentPane.add(goodname);
		goodname.setColumns(10);
		
		JLabel label_1 = new JLabel("单价:");
		label_1.setBounds(35, 100, 54, 15);
		contentPane.add(label_1);
		
		price = new JTextField();
		price.setBounds(99, 97, 212, 21);
		contentPane.add(price);
		price.setColumns(10);
		
		JLabel label_2 = new JLabel("单位:元");
		label_2.setBounds(321, 100, 54, 15);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("数量:");
		label_3.setBounds(35, 143, 54, 15);
		contentPane.add(label_3);
		
		number = new JTextField();
		number.setBounds(99, 140, 269, 21);
		contentPane.add(number);
		number.setColumns(10);
		
		JLabel label_4 = new JLabel("简介:");
		label_4.setBounds(35, 188, 54, 15);
		contentPane.add(label_4);
		
		message = new JTextArea();
		message.setBounds(99, 184, 269, 103);
		contentPane.add(message);
		
		JButton button = new JButton("确定添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//添加商品
				String gn=goodname.getText();
				String num=number.getText();
				String p=price.getText();
				String t=message.getText();
				System.out.println(gn+num+p+message);
				if("".equals(gn)||"".equals(num)||"".equals(price))
				{
					JOptionPane.showMessageDialog(null,"请输入","success", JOptionPane.NO_OPTION);
				}else{
					Goods g = null;
					try{
						g=new Goods(gn,Double.parseDouble(p),Integer.parseInt(num));
						g.setMessage(t);
					}catch(Exception e1){
						JOptionPane.showMessageDialog(null,"请正确输入","", JOptionPane.NO_OPTION);
						return;
					}
					//没有异常的话
					boolean msg=IFactory.addAGoods(g);
					if(msg)
					{
						JOptionPane.showMessageDialog(null,"添加商品成功","success", JOptionPane.NO_OPTION);
						frame.dispose();
						//关闭第一个窗口
						store.dispose();
						
						setVisible(false);
						AdminStore store=new AdminStore();
						store.setAdminlogin(adminlogin);
						store.setVisible(true);
					}else{
						JOptionPane.showMessageDialog(null,"添加商品失败","faile", JOptionPane.NO_OPTION);	
					}	
					
				}
				
				
			}
		});
		button.setBounds(275, 310, 93, 23);
		contentPane.add(button);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				((AdminAdd)e.getSource()).setVisible(false);
			}
		});
	}


}
